package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class AddressBook here.
 * 
 * @version (31 okt 2011)
 */
public class AddressBook
{
    private ArrayList<Person> myPersons;
    private Person setAllInformation;
    private static AddressBook instance;
    
    /**
     * The constructor for the AddressBook
     * @param myPersons A ArrayList of Persons
     */
    private AddressBook()
    {
        myPersons = new ArrayList<Person>();
    }
    
    /*
     * Singleton
     */
    public static AddressBook getInstance()
    {
        if(instance == null)
        {
            instance = new AddressBook();
        }
        return instance;
    }
    
    /**
     * Add a new person to the container
     * @param person Add a person to the AddressBook
     */
    public void createPerson(Person person)
    {
        myPersons.add(person);
    }
    
    /**
     * Delete a person from the AdressBook
     * @id The id of the person to delete from AddressBook
     */
    public void deletePerson(long id)
    {
        Person p = getPerson(id);
        myPersons.remove(p);
    }
    
    /**
     * Search for a person in the AddressBook
     * @param id The id of the person to find
     */
    public Person getPerson(long id)
    {
        int index = 0;
        boolean found = false;
        int index2 = 0;
        
        while (!found && index < myPersons.size())
        {
            Person person = myPersons.get(index);
            if(person.getId() == id)
            {
                found = true;
                index2 = index;
            }
            index++;
        }
        if(found == true)
        {
            return myPersons.get(index2);
        }
        else
        {
            return null;
        }
    }

        
    
    
    /**
     * Opdats the details of a person
     * @param id The persons id
     * @param name The name of the person
     * @param adress The persons adresse
     * @param The postal code of the adresse
     * @param city The name of the city
     * @param tlf The persons thelephone number
    */
    public void setPerson(long id, String name, String address, String postalCode, String city, String tlf)
    {
        Person p = getPerson(id);
        p.setAllInformation(id, name, address, postalCode, city, tlf);
        System.out.println(" ");
    }
    
    /**
     * Prints a total list of persons in AddressBook
     */
    public void listPersons()
    {
        for(Person person : myPersons)
        {
            person.print();
            System.out.println(" ");
        }
    }
    
    public void listPerson(long id)
    {
        for( Person person : myPersons)
        {
            if(person.getId() == id)
            {
                person.print();
            }
        }
    }
}
